﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


using FXnaEngine.Graphics;



namespace FXnaEngine
{


   public class FFileBasedSE:FShaderEffect
    {


        /// <summary>
        /// Content name for this shader
        /// </summary>
        private string shaderContentName = "";

  

        /// <summary>
        /// Effect handles for shaders.
        /// </summary>
        protected EffectParameter worldViewProj,
            viewProj,
            world,
            viewInverse,
            projection,

            ambientColor,
            diffuseColor,
            specularColor,
            specularPower

            ;




        //protected Color lastUsedAmbientColor = new Color(0,0,0,0);
        ///// <summary>
        ///// Ambient color
        ///// </summary>
        //public Color AmbientColor
        //{
        //    get
        //    {
        //        // Note: Only implemented for stupid FxCop rule,
        //        // you should never "get" a shader texture this way!
        //        return lastUsedAmbientColor;
        //    }
        //    set
        //    {
        //        SetValue(ambientColor, ref lastUsedAmbientColor, value);
        //    }
        //}






        #region Get parameters
        /// <summary>
        /// Get parameters, override to support more
        /// </summary>
        protected virtual void GetParameters()
        {
            worldViewProj = effect.Parameters["worldViewProj"];
            viewProj = effect.Parameters["viewProj"];
            world = effect.Parameters["world"];
            viewInverse = effect.Parameters["viewInverse"];
            projection = effect.Parameters["projection"];









        }
        #endregion

        #region SetParameters
        /// <summary>
        /// Set parameters, this overload sets all material parameters too.
        /// </summary>
        public virtual void SetParameters(FMaterial setMat)
        {





        }

        /// <summary>
        /// Set parameters, override to set more
        /// </summary>
        public virtual void SetParameters()
        {
            SetParameters(null);
        }

        /// <summary>
        /// Set parameters, this overload sets all material parameters too.
        /// </summary>
        public virtual void SetParametersOptimizedGeneral()
        {
            if (worldViewProj != null)
                worldViewProj.SetValue(FEngineState.WorldViewProjectionMatrix);
            if (viewProj != null)
                viewProj.SetValue(FEngineState.ViewProjectionMatrix);
            if (world != null)
                world.SetValue(FEngineState.WorldMatrix);
            if (viewInverse != null)
                viewInverse.SetValue(FEngineState.InverseViewMatrix);








            // lastUsed parameters for colors and textures are not used,
            // but we overwrite the values in SetParametersOptimized.
            // We fix this by clearing all lastUsed values we will use later.

            //lastUsedAmbientColor = new Vector4(0, 0, 0, 0);
            //lastUsedDiffuseColor = new Vector4(0, 0, 0, 0);
            //lastUsedSpecularColor = new Vector4(0, 0, 0, 0);
            //lastUsedDiffuseTexture = null;
            //lastUsedNormalTexture = null;


        }

        /// <summary>
        /// Set parameters, this overload sets all material parameters too.
        /// </summary>
        public virtual void SetParametersOptimized(FMaterial setMat)
        {
            if (setMat == null)
                throw new ArgumentNullException("setMat");

            // No need to set world matrix, will be done later in mesh rendering
            // in the MeshRenderManager. All the rest is set with help of the
            // SetParametersOptimizedGeneral above.

            // Only update ambient, diffuse, specular and the textures, the rest
            // will not change for a material change in MeshRenderManager.
            ambientColor.SetValue(setMat.Ambient);
            diffuseColor.SetValue(setMat.Diffuse);
            specularColor.SetValue(setMat.Specular);



        }
        #endregion



        #region Dispose
        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing">Disposing</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Dispose shader effect
                if (effect != null)
                    effect.Dispose();
            }
        }
        #endregion









    }


}
